{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mesh generation for tutorial 06, case 3b\n",
    "\n",
    "This file generates the mesh which is used in the following examples:\n",
    "* 3b_advection_diffusion_reaction_neumann_control\n",
    "\n",
    "The test case is from section 5.3 of\n",
    "```\n",
    "F. Negri, G. Rozza, A. Manzoni and A. Quarteroni. Reduced Basis Method for Parametrized Elliptic Optimal Control Problems. SIAM Journal on Scientific Computing, 35(5): A2316-A2340, 2013.\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gmsh\n",
    "from mpi4py import MPI\n",
    "from dolfinx.io import XDMFFile\n",
    "from multiphenicsx.mesh.utils import gmsh_to_fenicsx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert MPI.COMM_WORLD.size == 1, \"This mesh generation notebook is supposed to be run in serial\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Geometrical parameters and related quantities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lcar = 0.05"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create mesh with gmsh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class GenerateRectangleLines(object):\n",
    "    def __init__(self):\n",
    "        self.points = dict()\n",
    "        self.lines = dict()\n",
    "\n",
    "    def add_point(self, x, y):\n",
    "        key = (x, y)\n",
    "        try:\n",
    "            return self.points[key]\n",
    "        except KeyError:\n",
    "            p = gmsh.model.geo.addPoint(x, y, 0.0, lcar)\n",
    "            self.points[key] = p\n",
    "            return p\n",
    "\n",
    "    def add_line(self, p0, p1):\n",
    "        try:\n",
    "            return self.lines[(p0, p1)]\n",
    "        except KeyError:\n",
    "            l01 = gmsh.model.geo.addLine(p0, p1)\n",
    "            self.lines[(p0, p1)] = l01\n",
    "            self.lines[(p1, p0)] = -l01\n",
    "            return l01\n",
    "\n",
    "    def __call__(self, x_min, x_max, y_min, y_max):\n",
    "        p0 = self.add_point(x_min, y_min)\n",
    "        p1 = self.add_point(x_max, y_min)\n",
    "        if isinstance(y_max, list):\n",
    "            p2 = [self.add_point(x_max, y) for y in y_max]\n",
    "            p3 = self.add_point(x_min, y_max[-1])\n",
    "        else:\n",
    "            p2 = self.add_point(x_max, y_max)\n",
    "            p3 = self.add_point(x_min, y_max)\n",
    "        l0 = self.add_line(p0, p1)\n",
    "        if isinstance(y_max, list):\n",
    "            p1_p2 = [p1] + p2\n",
    "            l1 = [self.add_line(p1_p2[i], p1_p2[i + 1]) for i in range(len(p2))]\n",
    "            l2 = self.add_line(p2[-1], p3)\n",
    "        else:\n",
    "            l1 = self.add_line(p1, p2)\n",
    "            l2 = self.add_line(p2, p3)\n",
    "        l3 = self.add_line(p3, p0)\n",
    "        return (l0, l1, l2, l3)\n",
    "\n",
    "\n",
    "gmsh.initialize()\n",
    "gmsh.model.add(\"mesh\")\n",
    "generate_rectangle_lines = GenerateRectangleLines()\n",
    "[l0, l1, l2, l3] = generate_rectangle_lines(0.0, 1.0, 0.0, [0.3, 0.7, 1.0])\n",
    "[l4, l5, l6, _] = generate_rectangle_lines(1.0, 3.0, 0.0, 0.3)\n",
    "[_, l7, l8, _] = generate_rectangle_lines(1.0, 3.0, 0.3, 0.7)\n",
    "[_, l9, l10, _] = generate_rectangle_lines(1.0, 3.0, 0.7, 1.0)\n",
    "line_loop_rectangle_left = gmsh.model.geo.addCurveLoop([l0, l1[0], l1[1], l1[2], l2, l3])\n",
    "line_loop_rectangle_right_bottom = gmsh.model.geo.addCurveLoop([l4, l5, l6, -l1[0]])\n",
    "line_loop_rectangle_right_middle = gmsh.model.geo.addCurveLoop([-l6, l7, l8, -l1[1]])\n",
    "line_loop_rectangle_right_top = gmsh.model.geo.addCurveLoop([-l8, l9, l10, -l1[2]])\n",
    "rectangle_left = gmsh.model.geo.addPlaneSurface([line_loop_rectangle_left])\n",
    "rectangle_right_bottom = gmsh.model.geo.addPlaneSurface([line_loop_rectangle_right_bottom])\n",
    "rectangle_right_middle = gmsh.model.geo.addPlaneSurface([line_loop_rectangle_right_middle])\n",
    "rectangle_right_top = gmsh.model.geo.addPlaneSurface([line_loop_rectangle_right_top])\n",
    "gmsh.model.geo.synchronize()\n",
    "gmsh.model.addPhysicalGroup(1, [l0, l2, l3], 1)\n",
    "gmsh.model.addPhysicalGroup(1, [l4, l10], 2)\n",
    "gmsh.model.addPhysicalGroup(1, [l5, l7, l9], 3)\n",
    "gmsh.model.addPhysicalGroup(2, [rectangle_left], 1)\n",
    "gmsh.model.addPhysicalGroup(2, [rectangle_right_bottom, rectangle_right_top], 3)\n",
    "gmsh.model.addPhysicalGroup(2, [rectangle_right_middle], 2)\n",
    "gmsh.model.mesh.generate(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Convert to a dolfinx mesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mesh, subdomains, boundaries = gmsh_to_fenicsx(gmsh.model, gdim=2)\n",
    "gmsh.finalize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Save mesh, subdomains and boundaries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with XDMFFile(MPI.COMM_WORLD, \"graetz_2.xdmf\", \"w\") as file:\n",
    "    file.write_mesh(mesh)\n",
    "    file.write_meshtags(subdomains)\n",
    "    mesh.topology.create_connectivity(mesh.topology.dim - 1, mesh.topology.dim)\n",
    "    file.write_meshtags(boundaries)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython"
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
